package com.jinzhending.controller.wx;

import com.alibaba.fastjson.JSONObject;
import com.jinzhending.entity.Payload;
import com.jinzhending.entity.WxUser;
import com.jinzhending.service.wx.UserService;
import com.jinzhending.utils.*;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * @ProjectName: LieIdle
 * @Package: com.jinzhending.controller.wx
 * @Author: huat
 * @Date: 2020/4/9 8:52
 * @Version: 1.0
 */
@RestController
@RequestMapping("wx/user")
public class UserController {
    @Autowired
    RsaKeyProperties rsaKeyProperties;
    @Autowired
    UserService userService;
    /**
     * 退出
     * @param request
     * @return
     */
    @RequestMapping("loginOut")
    public Map<String,Object> loginOut(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String,Object>();

        String header = request.getHeader("Authorization").replace("bearer ","");
        Payload<WxUser> payload=JwtUtil.getInfoFromToken(header,rsaKeyProperties.getPublicKey(),WxUser.class);
        int result=userService.updateTokenById(payload.getUserInfo().getId(),"");
        if(result>0){
           map.put("code",0);
           map.put("data","");
            map.put("message","退出成功");
        }
        return map;
    }

    /**
     * 修改密码
     * @param request
     * @return
     */
    @RequestMapping("updatePassword")
    public Map<String,Object> updatePassword(HttpServletRequest request){
        Map<String,Object> map=new HashMap<String,Object>();
        String header = request.getHeader("Authorization").replace("bearer ","");
        Payload<WxUser> payload=JwtUtil.getInfoFromToken(header,rsaKeyProperties.getPublicKey(),WxUser.class);
        if(null==payload.getUserInfo()){
            map.put("code",1);
            map.put("data","");
            map.put("message","参数不合法");
            return map;
        }
        String encrypData=request.getParameter("data");
        String dataJson=AESUtil.decrypt(encrypData);
        JSONObject parseObject = JSONObject.parseObject(dataJson);
        String oldPassword=parseObject.get("oldPassword").toString();
        String newPassword=parseObject.get("newPassword").toString();
        if(!MD5Util.encode(oldPassword).equals(payload.getUserInfo().getPassword())){
            map.put("code",1);
            map.put("data","");
            map.put("message","原密码错误");
            return map;
        }
        int result=userService.updatePassword(payload.getUserInfo().getId(),newPassword);
        if(result<1){
            map.put("code",1);
            map.put("data","");
            map.put("message","修改失败");
        }else{
            map.put("code",0);
            map.put("data","");
            map.put("message","修改成功");
        }
        return map;
    }

//    @RequestMapping("logining")
//    public Map<String,Object> logining(HttpServletRequest request){
//        String header = request.getHeader("Authorization").replace("bearer ","");
//        Payload<WxUser> payload=JwtUtil.getInfoFromToken(header,rsaKeyProperties.getPublicKey(),WxUser.class);
//        System.out.println(payload.getUserInfo().getUsername());
//        return null;
//    }


}
